/**
 *                            License
 * THE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF THIS  
 * CREATIVE COMMONS PUBLIC LICENSE ("CCPL" OR "LICENSE"). 
 * THE WORK IS PROTECTED BY COPYRIGHT AND/OR OTHER APPLICABLE LAW.  
 * ANY USE OF THE WORK OTHER THAN AS AUTHORIZED UNDER THIS LICENSE OR  
 * COPYRIGHT LAW IS PROHIBITED.
 * 
 * BY EXERCISING ANY RIGHTS TO THE WORK PROVIDED HERE, YOU ACCEPT AND  
 * AGREE TO BE BOUND BY THE TERMS OF THIS LICENSE. TO THE EXTENT THIS LICENSE  
 * MAY BE CONSIDERED TO BE A CONTRACT, THE LICENSOR GRANTS YOU THE RIGHTS CONTAINED 
 * HERE IN CONSIDERATION OF YOUR ACCEPTANCE OF SUCH TERMS AND CONDITIONS.
 * 
 */
package l1j.server.server.clientpackets;

import java.util.Calendar;

import l1j.server.server.ClientThread;
import l1j.server.server.model.L1Object;
import l1j.server.server.model.L1World;
import l1j.server.server.model.Instance.L1MonsterInstance;
import l1j.server.server.model.Instance.L1NpcInstance;
import l1j.server.server.model.Instance.L1PcInstance;
import l1j.server.server.serverpackets.S_SendLocation;
import l1j.server.server.serverpackets.S_ServerMessage;
import l1j.server.server.utils.L1SpawnUtil;

public class C_SendLocation extends ClientBasePacket {

	private static final String C_SEND_LOCATION = "[C] C_SendLocation";

	public C_SendLocation(byte abyte0[], ClientThread client) {
		super(abyte0);
		int type = readC();

		// クライアントがアクティブ,ノンアクティブ转换时に
		// オペコード 0x57 0x0dパケットを送ってくるが详细不明の为无视
		// マップ座标转送时は0x0bパケット
		if (type == 0x0d) {
			return;
			/**视窗内:0d 01 xx // xx 就是值会变动，不知原因。
			视窗外:0d 00 xx // xx 就是值会变动，不知原因。**/
		}

		if (type == 0x0b) {
			String name = readS();
			int mapId = readH();
			int x = readH();
			int y = readH();
			int msgId = readC();

			if (name.isEmpty()) {
				return;
			}
			L1PcInstance target = L1World.getInstance().getPlayer(name);
			if (target != null) {
				L1PcInstance pc = client.getActiveChar();
				String sender = pc.getName();
				target.sendPackets(new S_SendLocation(type, sender, mapId, x, y,
						msgId));
				// 将来的にtypeを使う可能性があるので送る
				pc.sendPackets(new S_ServerMessage(1783, name));
			}
		} else if (type == 0x06) {
			int objectId = readD();
			int gate = readC();
			int unknow2 = readD();
			int dragonGate[] = { 81273, 81274, 81275, 81276 };
			L1PcInstance pc = client.getActiveChar();
			if (gate >=0 && gate <= 3) {
				Calendar nowTime = Calendar.getInstance();
				if (nowTime.get(Calendar.HOUR_OF_DAY) >= 8 && nowTime.get(Calendar.HOUR_OF_DAY) <= 12) {
					pc.sendPackets(new S_ServerMessage(1643)); // 每日上午 8 点到 12 点为止，暂时无法使用龙之钥匙。
				} else if (!pc.getInventory().checkItem(47010, 1)) {
					pc.sendPackets(new S_ServerMessage(74, "身上无龙之钥匙，"));
				} else {
					boolean found = false;
					for (L1Object obj : L1World.getInstance().getObject()) {
						if (obj instanceof L1NpcInstance) {
							L1NpcInstance mob = (L1NpcInstance) obj;
							if (mob != null) {
								int mobId = mob.getNpcTemplate().get_npcId();
								if (mobId >= dragonGate[0] && mobId <= dragonGate[3]) {
									if (mobId == dragonGate[gate]) {
										found = true;
										break;
									}
								}
							}
						}
					}
					if (found) {
						pc.sendPackets(new S_ServerMessage(74, "此龙之门扉已经开启，"));
					} else {
						pc.getInventory().consumeItem(47010, 1);
						L1SpawnUtil.spawn(pc, dragonGate[gate], 0, 120 * 60 * 1000);//开启 2 小时
					}
				}
			}
		}
	}

	@Override
	public String getType() {
		return C_SEND_LOCATION;
	}
}
